package com.mentaleye.biz.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.mentaleye.biz.StudentAttendanceRecordBiz;
import com.mentaleye.dal.model.StudentAttendanceRecord;
import com.mentaleye.service.StudentAttendanceRecordService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
import com.mentaleye.util.FileUtils;

@Service
public class StudentAttendanceRecordBizImpl implements StudentAttendanceRecordBiz {

	private static Logger logger = Logger.getLogger(StudentAttendanceRecordBiz.class);

	@Autowired
	private StudentAttendanceRecordService attendanceRecordService;

	@Override
	public CommonResult uploadExcel(MultipartFile multipartFile, HttpServletRequest request) {
		CommonResult result = new CommonResult();
		InputStream is = null;
		String fileName = null;
		String uploadPath = null;
		if (multipartFile.isEmpty()) {
			result.setResultMsg("上传文件不能为空");
		} else {
			try {
				is = multipartFile.getInputStream();
				fileName = FileUtils.getNewFileName(multipartFile.getOriginalFilename());
				uploadPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload/");
				FileUtils.upload(is, uploadPath, fileName);
				result.setIsSuccess(true);
				result.setData(fileName);
			} catch (IOException e) {
				result.setResultMsg("上传文件异常！");
				logger.error("上传Excel文件异常！", e);
			}
		}
		return result;
	}

	@Override
	public CommonResult updateRecord(StudentAttendanceRecord record) {
		CommonResult result = new CommonResult();
		try {
			if (this.attendanceRecordService.updateRecord(record)) {
				result.setIsSuccess(true);
				result.setResultMsg("更新成功！");
			} else {
				result.setResultMsg("更新失败！");
			}
		} catch (Exception e) {
			result.setResultMsg("更新异常！");
			logger.error("学生考勤信息更新异常！", e);
		}
		return result;
	}

	@Override
	public CommonResult updateByList(List<StudentAttendanceRecord> recordList) {
		CommonResult result = new CommonResult();
		Date writeTime = null;
		try {
			if (!recordList.isEmpty()) {
				writeTime = recordList.get(0).getWriteTime();
				if (this.attendanceRecordService.deleteByWriteTime(writeTime)) {
					if (this.attendanceRecordService.insertByList(recordList)) {
						result.setIsSuccess(true);
						result.setResultMsg("更新成功！");
					} else {
						result.setResultMsg("更新失败！");
					}
				} else {
					result.setResultMsg("更新失败！");
				}
			}
		} catch (Exception e) {
			result.setResultMsg("更新异常！");
			logger.error("学生考勤信息更新异常！", e);
		}
		return result;
	}

	@Override
	public void getListByPage(CommonPage page) {
		List<Map<String, Object>> recordList = null;
		try {
			recordList = this.attendanceRecordService.getListByMap(page.pageToMap());
			page.setPageData(recordList);
		} catch (Exception e) {
			logger.error("分页读取学生考勤信息异常！", e);
		}
	}
	
}
